package org.example.controller.impl;

import org.example.controller.CommentController;
import org.example.domain.Comment;
import org.example.domain.response.ResponseResult;
import org.example.services.CommentService;
import org.example.services.impl.CommentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/comments")
public class CommentControllerImpl implements CommentController {

    @Autowired
    private CommentServiceImpl commentService;

    @Override
    @PostMapping()
    @PreAuthorize("hasAuthority('comment:save')")
    public ResponseResult saveComment(@RequestBody @Validated(Comment.save.class) Comment comment) {
        return commentService.saveComment(comment);
    }

    @Override
    @DeleteMapping("/{commentId}")
    @PreAuthorize("hasAuthority('comment:delete')")
    public ResponseResult deleteCommentById(@PathVariable @NotNull(message = "评论ID不能为空") int commentId) {
        return commentService.deleteCommentById(commentId);
    }

    @Override
    @GetMapping("/4/{discussionId}/{currentPage}")
    @PreAuthorize("hasAuthority('comment:select')")
    public ResponseResult getCommentByDiscussionId(@PathVariable @NotNull(message = "评论ID不能为空") int discussionId,
                                                   @PathVariable @NotNull(message = "当前页数不能为空") int currentPage) {
        return commentService.getCommentByDiscussionId(discussionId,currentPage);
    }
}
